home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / programming / aros / exec / allocentry.c < prev    next >
C/C++ Source or Header  |  1996-09-13  |  3KB  |  125 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: allocentry.c,v 1.5 1996/09/13 17:51:22 digulla Exp $
  4.     $Log: allocentry.c,v $
  5.     Revision 1.5  1996/09/13 17:51:22  digulla
  6.     Use IPTR
  7.  
  8.     Revision 1.4  1996/08/13 13:55:57  digulla
  9.     Replaced __AROS_LA by __AROS_LHA
  10.     Replaced some __AROS_LH*I by __AROS_LH*
  11.     Sorted and added includes
  12.  
  13.     Revision 1.3  1996/08/01 17:41:04  digulla
  14.     Added standard header for all files
  15.  
  16.     Desc:
  17.     Lang:
  18. */
  19. #include "exec_intern.h"
  20. #include <aros/libcall.h>
  21.  
  22. /*****************************************************************************
  23.  
  24.     NAME */
  25.     #include <exec/memory.h>
  26.     #include <clib/exec_protos.h>
  27.  
  28. __AROS_LH1(struct MemList *, AllocEntry,
  29.  
  30. /*  SYNOPSIS */
  31.     __AROS_LHA(struct MemList *, entry, A0),
  32.  
  33. /*  LOCATION */
  34.     struct ExecBase *, SysBase, 37, Exec)
  35.  
  36. /*  FUNCTION
  37.     Allocate a number of memory blocks through a MemList structure.
  38.  
  39.     INPUTS
  40.     entry - The MemList with one MemEntry for each block you want to get
  41.  
  42.     RESULT
  43.     The allocation was successful if the most significant bit of the
  44.     result is 0. The result then contains a pointer to a copy of
  45.     the MemList structure with the me_Addr fields filled.
  46.     If the most significant bit is set the result contains the type of
  47.     memory that couldn't be allocated.
  48.  
  49.     NOTES
  50.  
  51.     EXAMPLE
  52.  
  53.     BUGS
  54.  
  55.     SEE ALSO
  56.     FreeEntry()
  57.  
  58.     INTERNALS
  59.  
  60.     HISTORY
  61.     18-10-95    created by m. fleischer
  62.  
  63. ******************************************************************************/
  64. {
  65.     __AROS_FUNC_INIT
  66.  
  67.     struct MemList *ret;
  68.     ULONG mlsize,i;
  69.  
  70.     /* Calculate size of a MemList with ml_NumEntries MemEntries. */
  71.     mlsize=sizeof(struct MemList)-sizeof(struct MemEntry)+
  72.        sizeof(struct MemEntry)*entry->ml_NumEntries;
  73.  
  74.     /* Get the MemList structure */
  75.     ret=(struct MemList *)AllocMem(mlsize,MEMF_PUBLIC);
  76.  
  77.     /* Check nasty case where the returncode is misleading :-( */
  78.     if((IPTR)ret&0x80ul<<(sizeof(APTR)-1)*8)
  79.     {
  80.     FreeMem(ret,mlsize);
  81.     ret=NULL;
  82.     }
  83.  
  84.     /* The allocation failed? Return "no public memory" */
  85.     if(ret==NULL)
  86.     return (struct MemList *)(MEMF_PUBLIC|0x80ul<<(sizeof(APTR)-1)*8);
  87.  
  88.     /* Init new struct */
  89.     ret->ml_NumEntries=entry->ml_NumEntries;
  90.     ret->ml_Node.ln_Type=0;
  91.     ret->ml_Node.ln_Pri =0;
  92.     ret->ml_Node.ln_Name=NULL;
  93.  
  94.     /* Fill all entries */
  95.     for(i=0;i<entry->ml_NumEntries;i++)
  96.     {
  97.     /* Get one */
  98.     ret->ml_ME[i].me_Addr=AllocMem(entry->ml_ME[i].me_Length,
  99.                        entry->ml_ME[i].me_Reqs);
  100.     /* Got it? */
  101.     if(ret->ml_ME[i].me_Addr==NULL)
  102.     {
  103.         /* No. Set returncode to "none of the 'ml_ME[i].me_Reqs' memory". */
  104.         entry=(struct MemList *)
  105.           ((IPTR)entry->ml_ME[i].me_Reqs|0x80ul<<(sizeof(APTR)-1)*8);
  106.  
  107.         /* Free everything allocated until now... */
  108.         for(;i-->0;)
  109.         FreeMem(ret->ml_ME[i].me_Addr,ret->ml_ME[i].me_Length);
  110.  
  111.         /* ...including the MemList */
  112.         FreeMem(ret,mlsize);
  113.  
  114.         /* All done */
  115.         return entry;
  116.     }
  117.     /* Copy the Length field */
  118.     ret->ml_ME[i].me_Length=entry->ml_ME[i].me_Length;
  119.     }
  120.     /* Everything filled. Return OK. */
  121.     return ret;
  122.     __AROS_FUNC_EXIT
  123. } /* AllocEntry */
  124.  
  125.